package com.st.controller;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.st.entity.Emp;
import com.st.entity.User;
import com.st.service.IDeptService;
import com.st.service.IEmpService;
import com.st.service.impl.DeptServiceImpl;
import com.st.service.impl.EmpServiceImpl;
import com.st.util.DateUtil;
import com.st.util.UploadUtil;
import com.sun.xml.internal.ws.api.addressing.AddressingVersion.EPR;

@WebServlet("/emp.do")
@MultipartConfig
public class EmpServlet extends BaseServlet{

	private IEmpService empService = new EmpServiceImpl();
	private IDeptService deptService = new DeptServiceImpl();
	
	public void search(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 1. 参数
		String ename = request.getParameter("ename");
		String ephone = request.getParameter("ephone");
		String sDid = request.getParameter("did");
		Integer did = null;
		
		if (sDid !=null && sDid.length()>0) {
			did = Integer.parseInt(sDid);
		}
		
		Emp emp = new Emp();
		emp.setEname(ename);
		emp.setEphone(ephone);
		emp.setDid(did);
 		
		// 2. 业务
		List emps = empService.search(emp);
		List depts = deptService.getAll();
		// 3. 结果
		request.setAttribute("emps", emps);
		request.setAttribute("depts", depts);
		request.getRequestDispatcher("empList.jsp").forward(request, response);
	}
	
	public void add(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		// 1. 参数
		String ename = request.getParameter("ename");
		String ephone = request.getParameter("ephone");
		Integer did = Integer.parseInt(request.getParameter("did"));
		Date ebirthday = DateUtil.strToDate(request.getParameter("ebirthday"), "yyyy-MM-dd");
		String ephoto = UploadUtil.saveFile(request, "ephoto", "upload");
		
		Emp emp = new Emp();
		emp.setEname(ename);
		emp.setEphone(ephone);
		emp.setDid(did);
		emp.setEbirthday(ebirthday);
		emp.setEphoto(ephoto);
		
		// 2. 业务
		int res = empService.add(emp);
		
		// 3. 结果
		if (res > 0 ) {
			// 跳转
			response.sendRedirect("empList.jsp");
		} else {
			request.setAttribute("error", "添加失败");
			request.getRequestDispatcher("empAdd.jsp").forward(request, response);
		}
	}
	
	public void searchJson(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// 1. 参数
		String ename = request.getParameter("ename");
		String ephone = request.getParameter("ephone");
		String sDid = request.getParameter("did");
		Integer did = null;
		
		if (sDid !=null && sDid.length()>0) {
			did = Integer.parseInt(sDid);
		}
		
		Emp emp = new Emp();
		emp.setEname(ename);
		emp.setEphone(ephone);
		emp.setDid(did);
		
		// 2. 业务
		List emps = empService.search(emp);
		// 3. 结果
		ObjectMapper objectMapper = new ObjectMapper();
		String json = objectMapper.writeValueAsString(emps);
		response.getWriter().write(json);
	}
	public void modify(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// 1. 参数
		String ename = request.getParameter("ename");
		String ephone = request.getParameter("ephone");
		String sDid = request.getParameter("did");
		Integer did = null;

		if (sDid !=null && sDid.length()>0) {
			did = Integer.parseInt(sDid);
		}

		Emp emp = new Emp();
		emp.setEname(ename);
		emp.setEphone(ephone);
		emp.setDid(did);

		// 2. 业务
		List emps = empService.search(emp);
		// 3. 结果
		ObjectMapper objectMapper = new ObjectMapper();
		String json = objectMapper.writeValueAsString(emps);
		response.getWriter().write(json);
	}
	public void remove(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String id = request.getParameter("eid");
		int eid = Integer.parseInt(id);
		int depts = empService.remove(eid);
		response.setContentType("text/html; charset=utf-8");
		if (depts > 0) {
			// 删除成功
			response.getWriter().write("<script>alert('删除成功'); window.location.href='empList.jsp';</script>");
		} else {
			// 删除失败
			response.getWriter().write("<script>alert('删除失败'); window.location.href='empList.jsp';</script>");
		}
	}
}
